/**
 * 2018/6/29 10:07:21 Wen Jun created.
 */

package com.diver.flinkdemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diver.flinkdemo.entity.xxt.Fence;
import com.diver.flinkdemo.entity.xxt.School;
import com.diver.flinkdemo.entity.xxt.SecurityArea;
import com.diver.flinkdemo.entity.xxt.Wifi;
import com.diver.flinkdemo.mapper.mysqlMapper.FenceMapper;
import com.diver.flinkdemo.mapper.mysqlMapper.SecurityAreaMapper;
import com.diver.flinkdemo.service.ISchoolService;
import com.diver.flinkdemo.service.ISecurityAreaService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 安全区域 Service 实现
 * Created by Wen Jun on 2018/06/29.
 */
@Slf4j
//@Service
public class SecurityAreaService implements ISecurityAreaService {
    Logger logger = LoggerFactory.getLogger(getClass());

//    @Autowired
    private ISchoolService schoolService = new SchoolService();
    private FenceMapper mapper;
    private SqlSession session;

    @Override
    public List<Wifi> getWifiInfoByGuardSettingAreaId(String areaId) {

        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession(true);
            mapper = session.getMapper(FenceMapper.class);
            Fence fence = mapper.selectById(areaId);
            log.info("----securityArea:{}",fence);
            if (fence!=null) {
                if (StringUtils.isNotBlank(fence.getWifiList())) {
                    return JSON.parseArray(fence.getWifiList(),Wifi.class);
                }
            }
            return null;
        } finally {
            endOfSession(session);
        }
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }
}
